package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"onerk4/zk1/api/enit/request"
	"onerk4/zk1/api/enit/response"
	"onerk4/zk1/dao/mysql"
	"onerk4/zk1/model"
)

func CreateOrder(c *gin.Context) {
	var req request.CreateOrder

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	hotel, err := mysql.ShowHotel(req.HotelId)
	if err != nil {
		return
	}

	fmt.Println(hotel.HotelTitle, hotel.Price)

	userId := c.GetUint("userId")
	orderNum := uuid.New().String()

	o := &model.Order{
		Model:         gorm.Model{},
		UserId:        int(userId),
		OrderNum:      orderNum,
		HotelId:       req.HotelId,
		HotelTitle:    hotel.HotelTitle,
		RoomType:      req.RoomType,
		GuestsStaying: req.GuestsStaying,
		CheckInDate:   req.CheckInDate,
		Price:         hotel.Price,
		Status:        1,
	}
	err = mysql.CreateOrder(o)
	if err != nil {
		response.ErrorResponse(c, "创建订单失败", err.Error())
		return
	}

	response.SuccessResponse(c, "创建订单成功", nil)
}

func ListOrder(c *gin.Context) {
	list, err := mysql.ShowList()
	if err != nil {
		return
	}

	var orders []*model.Order

	for _, hotel := range list {
		orders = append(orders, &model.Order{
			OrderNum:      hotel.OrderNum,
			HotelTitle:    hotel.HotelTitle,
			RoomType:      hotel.RoomType,
			GuestsStaying: hotel.GuestsStaying,
			CheckInDate:   hotel.CheckInDate,
			Price:         hotel.Price,
			Status:        hotel.Status,
		})
	}

	fmt.Println(orders)
	response.SuccessResponse(c, "展示成功", orders)
}
